<?php
//厦门大学计算机专业 | 前华为工程师
//专注《零基础学编程系列》  http://lblbc.cn/blog
//包含：Java | 安卓 | 前端 | Flutter | iOS | 小程序 | 鸿蒙
//公众号：蓝不蓝编程
declare (strict_types = 1);

namespace app\controller;

use app\JwtUtils;
use app\model\OrderInfo;
use app\model\UserOrder;
use think\facade\Db;
use think\facade\Request;

class McdonaldsController
{
    public function queryCategory()
    {
        $data = Db::table('m_category')->select();

        return json([
            'code' => 0,
            'msg' => "",
            'data' => $data,
        ]);
    }

    public function queryGoodsByCategory($categoryId)
    {
        $categoryId = $_REQUEST['categoryId'];
        $data = Db::name('m_goods')
            ->alias('g')
            ->join('m_goods_category gc', 'gc.goods_id=g.id AND gc.category_id=' . $categoryId)
            ->field('g.id, g.name, g.price, g.square_pic squarePic, g.description')
            ->select();
        return json([
            'code' => 0,
            'msg' => "",
            'data' => $data,
        ]);
    }

    public function searchGoods($keyword)
    {
        $keyword = $_REQUEST['keyword'];
        $data = Db::table('m_goods g')->where("name", "like", "%$keyword%")
            ->field('g.id, g.name, g.price, g.square_pic squarePic, g.description')
            ->select();
        return json([
            'code' => 0,
            'msg' => "",
            'data' => $data,
        ]);
    }

    public function queryGoods($goodsId)
    {
        $data = Db::table('m_goods g')->where("id", "=", $goodsId)
            ->field('g.id, g.name, g.price, g.square_pic squarePic, g.description')
            ->select();
        return json([
            'code' => 0,
            'msg' => "",
            'data' => $data[0],
        ]);
    }

    public function queryByStatus()
    {
        $authorization = Request::header('Authorization');
        $userId = JwtUtils::getUserIdFromToken($authorization);
        $userOrderList = Db::table('m_user_order')
            ->where('user_id=' . $userId)
            ->field('id orderId,status,create_time createTime')
            ->select()
            ->toArray();

        for ($i = 0; $i < count($userOrderList); $i++) {
            $userOrder = $userOrderList[$i];
            $sql = 'SELECT uo.id orderId,uo.status,g.id goodsId,g.name,g.price,g.square_pic squarePic,o.quantity FROM m_user_order uo,m_orders o,m_goods g WHERE uo.id=o.order_id AND o.goods_id=g.id AND o.order_id=?';
            $orderInfo = Db::query($sql, [$userOrder["orderId"]]);
            $userOrderList[$i]["list"] = $orderInfo;
        }
        return json([
            'code' => 0,
            'msg' => "",
            'data' => $userOrderList,
        ]);
    }

    public function queryOrder($orderId)
    {
        $userOrderList = Db::table('m_user_order')
            ->where('id','=', $orderId)
            ->field('id orderId,status,create_time createTime')
            ->select()
            ->toArray();

        for ($i = 0; $i < count($userOrderList); $i++) {
            $userOrder = $userOrderList[$i];
            $sql = 'SELECT uo.id orderId,uo.status,g.id goodsId,g.name,g.price,g.square_pic squarePic,o.quantity FROM m_user_order uo,m_orders o,m_goods g WHERE uo.id=o.order_id AND o.goods_id=g.id AND o.order_id=?';
            $orderInfo = Db::query($sql, [$userOrder["orderId"]]);
            $userOrderList[$i]["list"] = $orderInfo;
        }

        return json([
            'code' => 0,
            'msg' => "",
            'data' => $userOrderList[0],
        ]);
    }

    public function createOrder()
    {
        $authorization = Request::header('Authorization');
        $userId = JwtUtils::getUserIdFromToken($authorization);
        $requestData = Request::post();
        $simpleCartItemList = $requestData["simpleCartItemList"];

        $orderId = $this->uuid();
        $userOrderBean["id"] = $orderId;
        $userOrderBean["user_id"] = $userId;
        $userOrderBean["status"] = 1;
        $userOrderBean["create_time"] = time()*1000;
        $create = UserOrder::create($userOrderBean);

        for ($i = 0; $i < count($simpleCartItemList); $i++) {
            $orderInfo["id"] = $this->uuid();
            $orderInfo["goods_id"] = $simpleCartItemList[$i]["goodsId"];
            $orderInfo["quantity"] = $simpleCartItemList[$i]["quantity"];
            $orderInfo["order_id"] = $orderId;
            $create = OrderInfo::create($orderInfo);
        }
        $resp["orderId"] = $orderId;
        return json([
            'code' => 0,
            'msg' => "",
            'data' => $resp,
        ]);
    }

    public function deleteOrder($orderId)
    {
        OrderInfo::where('order_id', $orderId)->delete();
        UserOrder::where('id', $orderId)->delete();
        return json([
            'code' => 0,
            'msg' => "",
        ]);
    }

    public function uuid()
    {
        return md5(time() . mt_rand(1, 1000000));
    }
}
